package cn.hloger.spider.demo.dataxDemo.utils; /**
 * TODO
 *
 * @Description TODO
 * @author huanglong
 * @data 2024/7/15
 */

import org.apache.commons.lang3.StringUtils;

import java.lang.management.ManagementFactory;

/**
 * 进程ID单例封装<br>
 * 第一次访问时调用{@link ManagementFactory#getRuntimeMXBean()}获取PID信息，之后直接使用缓存值
 *
 * @author changxy
 */
public enum Pid {
    INSTANCE;

    private final int pid;

    Pid() {
        this.pid = getPid();
    }

    /**
     * 获取PID值
     *
     * @return pid
     */
    public int get() {
        return this.pid;
    }

    /**
     * 获取当前进程ID，首先获取进程名称，读取@前的ID值，如果不存在，则读取进程名的hash值
     *
     * @return 进程ID
     */
    private static int getPid() {
        final String processName = ManagementFactory.getRuntimeMXBean().getName();
        if (StringUtils.isBlank(processName)) {
            throw new NullPointerException("Process name is blank!");
        }
        final int atIndex = processName.indexOf('@');
        if (atIndex > 0) {
            return Integer.parseInt(processName.substring(0, atIndex));
        } else {
            return processName.hashCode();
        }
    }
}
